package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.constant.HttpStatus;
import com.zzyl.common.core.domain.entity.SysRole;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.dto.AlertDataDto;
import com.zzyl.nursing.dto.AlertDataPageReqDTO;
import com.zzyl.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 报警数据Service业务层处理
 *
 * @author alexis
 * @date 2025-03-11
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;
    @Autowired
    private ISysRoleService roleService;

    private final Integer PROCESSING_STATUS = 1;
    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return alertDataMapper.selectById(id);
    }



    /**
     * 查询报警数据列表
     *
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData) {
        return  alertDataMapper.selectAlertDataList(alertData);
    }
//    @Override
//    public TableDataInfo<AlertData> selectAlertDataList(AlertDataPageReqDTO dto)
//    {
//        //MP的page插件
//        Page<AlertData> alertDataPagePage = new Page<>(dto.getPageNum(), dto.getPageSize());
//        //MP查询数据库
//        this.lambdaQuery().eq(AlertData::getDeviceName, dto.getDeviceName())
//                .eq(AlertData::getStatus, dto.getStatus())
//                .between(AlertData::getCreateTime, dto.getStartTime(), dto.getEndTime())
//                .page(alertDataPagePage);
//        //返回分页数据
//        TableDataInfo<AlertData> tableDataInfo = new TableDataInfo<>();
//        tableDataInfo.setMsg("查询数据成功");
//        tableDataInfo.setCode(HttpStatus.SUCCESS);
//        tableDataInfo.setTotal(alertDataPagePage.getTotal());
//        tableDataInfo.setRows(alertDataPagePage.getRecords());
//        return tableDataInfo;
//    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return alertDataMapper.insert(alertData);
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return alertDataMapper.updateById(alertData);
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return alertDataMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return alertDataMapper.deleteById(id);
    }

    /**
     * 处理设备报警数据
     */
    @Override
    public void handleAlertData(AlertDataDto dto) {
        //1、根据id查询当前数据
        AlertData alertData = this.lambdaQuery().eq(AlertData::getId, dto.getId()).one();
        if (ObjUtil.isNull(alertData)) {
            throw new BaseException("当前设备不存在");
        }
        //处理前端数据，将yyyyMMdd'T'HHmmss'Z'转换成 中国/上海 时区时间
        String time = dto.getProcessingTime();
        LocalDateTime parseTime = LocalDateTimeUtil.parse(time, "yyyy-MM-dd'T'HH:mm:ss.'000Z'");
        LocalDateTime processingTime = parseTime.atZone(ZoneId.from(ZoneOffset.UTC))
                .withZoneSameInstant(ZoneId.of("Asia/Shanghai"))
                .toLocalDateTime();
        //获取当前登录账号id
        Long userId = SecurityUtils.getUserId();
        //获取登录账号name
        SysRole sysRole = roleService.selectRoleById(userId);
        //设置处理结果，处理时间，处理人，处理状态
        alertData.setProcessingResult(dto.getProcessingResult());
        alertData.setProcessingTime(processingTime);
        alertData.setStatus(PROCESSING_STATUS);
        alertData.setProcessorName(sysRole.getRoleName());
        updateAlertData(alertData);
    }
}
